<template>
    <div >
        <!--搜索表单-->
        <div style="margin-bottom: 20px">
            <el-input style="width:240px" placeholder="请输入名称" v-model="params.username"></el-input>
            <el-input style="width:240px ;margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
            <el-input style="width:240px ;margin-left: 5px" placeholder="请输入邮箱" v-model="params.email"></el-input>
            <el-button style="margin-left: 5px " type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
            <el-button style="margin-left: 5px " type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>
        </div>

        <el-table :data="tableData" stripe>

            <el-table-column prop="username" label="名称"></el-table-column>
            <el-table-column prop="phone" label="联系方式"></el-table-column>
            <el-table-column prop="email" label="邮箱"></el-table-column>

            <el-table-column prop="head" label="头像">
              <template v-slot="scope">
                <el-avatar :size="70" :src="scope.row.head"></el-avatar>

              </template>
            </el-table-column>
            <el-table-column prop="identity" label="身份信息"></el-table-column>
            <el-table-column prop="createtime" label="创建时间"></el-table-column>

            <el-table-column label="状态" width="120">
                <template v-slot="scope">
                    <el-switch
                            v-model="scope.row.status"
                            @change="changeStatus(scope.row)"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="scope">
                    <!--scope就是当前行的数据-->     <!--点击跳转到这个路由，带上ID-->
                <el-button type="primary"  @click="$router.push('/EditAdmin?id=' +scope.row.id)">编辑</el-button>
                    <el-popconfirm
                            style="margin-left: 5px"
                            title="确认删除这行数据吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button  type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                    <el-button style="margin-left: 5px"  type="warning" slot="reference" @click="handleChangePass(scope.row)">修改密码</el-button>

                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 10px">
            <el-pagination
                    background
                    :page-size="params.pageSize"
                    :current-page="params.pageNum"
                    layout="prev, pager, next"
                    @current-change="handleCurrentChange"
                    :total="total">

            </el-pagination>
        </div>
        <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="30%">
            <el-form :model="form" label-width="100"px ref="formRef" :rules="rules">
                <el-form-item label="新密码" prop="newPass">
                    <el-input v-model="form.newPass" autocomplete="off" show-password></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="savePass">确 定</el-button>
            </div>
        </el-dialog>
        <!--分页-->

    </div>
</template>

<script>
    import request from "@/util/request";
    import Cookies from 'js-cookie';
    export default {
        name: 'Admin',
        data(){
            return{
                tableData:[],
                total: 0,
                form:{},
                dialogFormVisible:false,
                admin:Cookies.get('admin') ? JSON.parse(Cookies.get('admin')):{},
                //设置分页查询url携带的参数： http://localhost:9090/user/page?pageNum=1&pageSize=10&name=“”&phone=“”
                params:{
                    pageNum:1,
                    pageSize:5,
                    name:"",
                    phone:"",
                    email:""
                },
                rules: {
                    newPass: [
                        {required: true, message: '请输入新密码', trigger: 'blur'},
                        {min:3,max:10,message: "长度在3-10个字符",trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            this.load()
        },
        methods:{
            changeStatus(row){
                if (this.admin.id==row.id &&!row.status){
                    row.status=true
                    this.$notify.warning("您的操作不合法")
                    return
                }
                request.put("/admin/updateStatus",row).then(res=>{
                    if (res.code=='200'){
                        this.$notify.success("操作成功")
                        this.load()
                    }else{
                        this.$notify.error(res.msg)
                    }

                })
            },

            handleChangePass(row){
            this.form=JSON.parse(JSON.stringify(row))
                this.dialogFormVisible=true
            },
            savePass(){
                this.$refs['formRef'].validate((valid) => {
                    if (valid) {
                 request.put("/admin/password",this.form).then(res=>{
                    if (res.code=='200'){
                        this.$notify.success("修改成功")
                        this.dialogFormVisible=false
                        if (this.form.id==this.admin.id){   //当前修改用户的id是否为当前登录用户的id
                            //是的话修改成功跳转到登录页面
                            this.$router.push("/login")
                            //并清楚cookies里面的数据
                            Cookies.remove("admin")
                        }
                    }else{
                        this.$notify.error("修改失败")
                    }
                })

                    }
                })
            },
            load(){
                // fetch("http://localhost:9090/user/page").then(res=>res.json()).then(res=>{
                //     console.log()
                //     this.tableData = res
                // })
                //使用封装好的request.js
                request.get('/admin/page',{params:this.params}).then(res => {
                    if(res.code =="200"){
                        this.tableData=res.data.list
                        this.total=res.data.total
                    }
                })
            },
            reset(){
                //重置按钮：直接把数据设置成默认
                this.params={
                    pageNum:1,
                    pageSize:10,
                    name:"",
                    phone:"",
                    email:""
                }
                //再次调用查询所有
                this.load();
            },
            handleCurrentChange(pageNum){
                //点击分页按钮触发
                this.params.pageNum=pageNum;
                this.load();
            },
            del(id){
                request.delete('/admin/delete/'+id).then(res=>{
                    if(res.code=="200"){
                        this.$notify.success("删除成功")
                        this.load()
                    }else{
                        this.$notify.error(res.msg)
                    }
                })
            }
        }

    }
</script>

<style scoped>

</style>
